package com.dx.wx.controller;

import com.dx.wx.service.DxProductService;
import com.dx.wx.service.DxShopService;
import com.dx.wx.service.DxUserService;
import com.ejlchina.data.Array;
import com.ejlchina.data.Mapper;
import com.yf.annotation.IgnoreAuth;
import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import com.yf.response.RRException;
import lombok.Data;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/dx/link")
@Data
public class ApiDxLinkController {

    /**
     * @Value无法读取数组 可用 @ConfigurationProperties(prefix = "wx.mini.bb") 替换
     *
     */
    @Value("#{'${wx.mini.bb.types}'.split(',')}")
    private List<String> types;


    @Autowired
    private DxShopService dxShopService;



    @PostMapping("/save")
    public R save(@LoginUserId Long phone,@RequestBody  Map map,String appId){
        dxShopService.saveLink(phone, map,appId);
        return R.ok();
    }

    /**
     * 从连锁产品分担中查询
     * @return
     */
    @GetMapping("/my")
    public R myLink(@LoginUserId Long phone,String otherPhone,String appId){
        String sePhone = String.valueOf(phone);
        if(otherPhone!=null&&!otherPhone.equals("")){
            sePhone = otherPhone;
        }
        Array array = dxShopService.findMyLink(Long.valueOf(sePhone),appId);
//        Mapper userMapper = dxUserService.findByMobile(String.valueOf(phone), appId);
//        if (userMapper.getInt("field_Rh3vp__c") == 5||userMapper.getInt("field_Rh3vp__c") == 6){//主管的话把经理查出来
//
//        }
        Array topList =  dxUserService.findByTop(Long.valueOf(sePhone),appId);
        return R.ok(array.toString()).putExtra("topList",topList.toString());
    }

    @Resource
    private DxUserService dxUserService;

    @PostMapping("/date")
    public R save(@RequestBody  Map map){
        Mapper mapper = dxShopService.findShopById((String) map.get("_id"));
        if(mapper.getInt("status__c")!=1){
            throw new RRException("禁止延期");
        }
        //设置为待审核
        map.put("delay_status__c",0);
        dxShopService.update(map);
        return R.ok();
    }

    /**
     * 配送形式
     * @return
     */
    @GetMapping("/send-type")
    public R sendType(){
        return R.ok(types);
    }

    /**
     * 选择产品
     * @return
     */
    @GetMapping("/products")
    public R products(String appId){
      Array array =   productService.findProduct(appId);
      return R.ok(array.toString());
    }

    /**
     * 根据自己的级别筛选
     * @param phone
     * @param appId
     * @return 从开发申请表
     */
    @GetMapping("/")
    public R findLink(@LoginUserId Long phone,String appId,@RequestParam(required = false) Long startDate,@RequestParam(required = false) Long endDate){
        Array array =  dxShopService.findLink(phone,appId,startDate,endDate);
        //把下级传过来
        Array users = dxUserService.findByTop(phone,appId);
        return R.ok(array.toString()).putExtra("users",users.toString());
    }

    /**
     * 从那个客户表
     * @param phone
     * @param appId
     * @return
     */
    @GetMapping("/list")
    public R listLink(@LoginUserId Long phone,String appId){
        Array array = dxShopService.findLink(phone,appId);
        return R.ok(array.toString());
    }


    /**
     * 从那个客户表
     * @param phone
     * @param appId
     * @return
     */
    @GetMapping("/list/{name}")
    public R listLinkByName(String appId, @PathVariable String name){
        Array array =  dxShopService.findLinkListByName(appId,name);
        return R.ok(array.toString());
    }



    @GetMapping("/info")
    public R getLinkIno(String id,String name,String appId){
        Mapper mapper;
        if(id!=null&&!id.equals("")){
             mapper = dxShopService.findShopById(id);
        }else {
             mapper = dxShopService.findLinkByName(name,appId);
             if(mapper==null){
                 return R.ok();
             }
        }
        return R.ok(mapper.toString());
    }

    @Resource
    private DxProductService productService;




//    //通过名字搜索客户
//    @GetMapping("/name")
//    @IgnoreAuth
//    public R findByName(String name,String appId){
//      Array array =  dxShopService.findShopByName(name,appId);
//      return R.ok(array.toString());
//    }




//    @GetMapping("/{id}")
//    public R findShop(@LoginUserId Long phone,@PathVariable String id){
//        Mapper mapper =  dxShopService.findShopById(id);
//        Array prices = dxShopService.findPriceByName(mapper.getString("name"));
//        Array tempGoods = dxShopService.findTempGoods(id);
//        return R.ok(mapper.toString()).putExtra("prices",prices.toString()).putExtra("tempGoods",tempGoods.toString());
//    }

    //查找自己的直接客户
    @GetMapping("/top")
    public R findByTop(@LoginUserId Long phone){
        Array array =  dxShopService.findShopByTop(phone);
        return R.ok(array.toString());
    }

    //查找连锁下面药店
    @PostMapping("/link")
    public R findByLinkShop(@LoginUserId Long phone,String[] linkIds){
        Array array =  dxShopService.findShopByLinkShop(linkIds);
        return R.ok(array.toString());
    }

    //获取价目表
    @GetMapping("/price")
    @IgnoreAuth
    public R findPrice(@LoginUserId Long phone,String shopId){
        Array array = dxShopService.findPrice(phone);
        return R.ok(array);
    }

    //获取可售产品
    @GetMapping("/goods")
    @IgnoreAuth
    public R findGoods(@LoginUserId Long phone){
        Array array = dxShopService.findPrice(phone);
        return R.ok(array);
    }


    //根据药店ID获取
    @GetMapping("/goods/{id}")
    @IgnoreAuth
    public R findGoodsById(@PathVariable String id){
        Array array = dxShopService.findPriceByShopId(id);
        return R.ok(array.toString());
    }


    //更新药店
    @PostMapping("/update")
    public R update(@RequestBody Map map){
        dxShopService.update(map);
        return R.ok();
    }

    //更新药店负责人
    @PostMapping("/update/owner")
    public R updateOwner(String userId,String[] shopIds){
        dxShopService.updateOwner(userId, shopIds);
        return R.ok();
    }




}
